var precontract={
	partInfoDom:$("#J_info"),
	isTS:false,
	status:false,
	init:function(page){
		var _t=this;

		_t.isTS=tianshi;
		$("#autoSelect").on("click",function(event){
			event.stopPropagation();
			return false;
		})
		_t.partInfoDom.find(".chooseAuto").on("click",function(event){
			event.stopPropagation();

			return false;
		})

		//if($("#J_submitData").html()=="提交预约"){

			if(_t.partInfoDom.find("input[name='companyId']").val()!=""){
				$("#J_jiyou").before($("#J_company"));
				$("#companyList").css("top","150px");
			}

			if(_t.partInfoDom.find("input[name='realname']").val()!=""){
				$("#J_jiyou").before($("#J_name"));
			}

			if(_t.partInfoDom.find("input[name='phone']").val()!=""){
				$("#J_jiyou").before($("#J_phone"));
			}
			
			if(_t.partInfoDom.find("input[name='time']").val()!=""){
				$("#J_jiyou").before($("#J_time"));
			}


			if(_t.partInfoDom.find("ul").eq(0).find("li").length==1){
				_t.partInfoDom.find("ul").eq(0).remove();
			}
		//}

		//输入手机后，在没有选择维修厂时异步获取数据
		_t.partInfoDom.find("input[name='phone']").on("focusout",function(){
			var phone=$(this).val();
			if(/^(13|15|18|14|17)[0-9]{9}$/.test(phone)){
				var companyId=_t.partInfoDom.find("input[name='companyId']");
				if(companyId==""){
					$.ajax({
						url:"/Index/findVip/",
						type:"post",
						dataType:"json",
						data:{"phone":phone},
						success:function(res){
							if(res.status==1){

							}
						}
					})
				}
			}
		})


		$("body").on("click",function(event){
			$("#autoSelect").removeClass("show");
		})
	},
	

	getPartInfo:function(id){
		var url="/Index/getPartInfo/",
			data={"pj_id":id},
			_t=this;
		$.ajax({
			url:url,
			type:"post",
			dataType:"json",
			data:data,
			success:function(res){
				if(res.status==1){

					if(typeof _t.partInfoDom[0]=="undefined"){	//当前在配件单管理页时执行添加配件条目事件
						report.addPartItem(res.data);
					}else{
						_t.printPartInfo(res.data);
					}
				}else{
					alert(res.info);
				}
			}
		})
	},
	submit:function(){
		var _t=this,
			data={};

		data['phone']=_t.partInfoDom.find("input[name='phone']").val();
		data['realname']=_t.partInfoDom.find("input[name='realname']").val();
		data['time']=_t.partInfoDom.find("input[name='time']").val();
		data['companyId']=_t.partInfoDom.find("input[name='companyId']").val();
		data['companyName']=_t.partInfoDom.find("#J_companyName").html();
		data['companyAddress']=_t.partInfoDom.find("input[name='companyAddress']").val();
		data['autoId']=_t.partInfoDom.find("input[name='model_id']").val();
		data['hh']=_t.partInfoDom.find("select option:selected").val();
		data['source']=3;
		data['v_id']=$("#J_vId").val();

		if(data['companyId']==0){
			alert("请选择一家您中意的维修厂");
			_t.showCompanyList();
			return false;
		}else if(data['realname']==""){
			alert("请输入您的姓名");
			_t.partInfoDom.find("input[name='realname']").focus();
			return false;
		}else if(!/^(13|15|18|14|17)[0-9]{9}$/.test(data['phone'])){
			alert("请输入正确的手机号");
			_t.partInfoDom.find("input[name='phone']").focus();

			return false;
		}else if(data['time']==""){
			alert("请输入预约时间");
			_t.partInfoDom.find("input[name='time']").focus();
			return false;
		}else if(data['hh']==""){
			alert("请选择具体时间段");
			_t.partInfoDom.find("select").focus();
			return false;
		}else if(data['autoId']==""){
			alert("请选择您爱车的具体车型");
			_t.showChooseAuto();
			return false;
		}

		$.ajax({
			url:"/Index/saveData/",
			type:"post",
			dataType:"json",
			data:data,
			success:function(res){
				alert(res.info);
				if(res.status==1){
					$("#J_txt").html("");
					if(res.data>1){
						$("#J_vId").val(res.data);
					}
					$("#J_submitData").addClass("btnGray");
					//$("#J_info").hide();
				}
			}
		})
	},
	tipMsg:function(){

	},

	showCompanyList:function(){
		if($("#companyList").hasClass("show")){
			$("#companyList").removeClass("show");
		}else{
			$("#companyList").addClass("show");
		}
		
	},

	chooseCompany:function(id,obj){
		obj=$(obj);
		obj.addClass("current").siblings("li").removeClass("current");
		$("#J_companyName").html(obj.find(".name").html());
		this.partInfoDom.find("input[name='companyId']").val(id);
		this.partInfoDom.find("input[name='companyAddress']").val(obj.find(".address").html());
		$("#companyList").removeClass("show");
	},

	//显示
	showChooseAuto:function(){
		var autoSelect=$("#autoSelect");

		if(autoSelect.hasClass("show")){
			$("#autoSelect").removeClass("show");
			var list=autoSelect.find(".list"),
				step=list.attr("data-step");
			list.attr("data-step",0).css({"marginLeft":"0px"});
		}else{
			$("#autoSelect").addClass("show");
		};
		return false;
	},


	//选择车型	step=0：查品牌；step=1：查车型；step=2:查排量；step=3:查年份
	chooseAutoCurrent:{"brandId":"","cxModel":"","cx_displacement":"","cx_year":""},
	chooseAuto:function(id,step){

		//计算每一步需要查询的条件
		var arr=["brandId","cxModel","cx_displacement","cx_year"],
			_t=this,
			data={};
		if(step==0){
			this.chooseAutoCurrent={"brandId":"","cxModel":"","cx_displacement":"","cx_year":""};
		}else{
			this.chooseAutoCurrent[arr[step-1]]=id;
		}

		for(var i=0;i<=step;i++){
			if(this.chooseAutoCurrent[arr[i]]!=""&&i<step){
				data[arr[i]]=this.chooseAutoCurrent[arr[i]];
			}
		}

		var list=$("#autoSelect > .list"),
			url="/Index/getAuto";

		$.ajax({
			type:"post",
			dataType:"json",
			url:url,
			data:data,
			success:function(res){
				if(res.data==null){
					html="<li>&nbsp;无相关数据，请重新选择</li>";
				}else{
					var html=step==0?_t.getBrandListHtml(res.data,step):_t.getAutoListHtml(res.data,step);
				}

				list.find("ul").eq(step).html(html);
				list.css({"marginLeft":"-"+(step*200)+"px"}).attr("data-step",step).next("a").show();
			}
		})

		if(step==0){
			var select=$(".autoSelect");
			if(select.hasClass("show")){
				select.removeClass("show");
			}else{
				select.addClass("show");
			}
		}

		return false;
	},

	//转换车型数据成HTML
	getBrandListHtml:function(data,step){
		var html="",
			len=data.length,
			i=0;
		for(;i<len;i++){
			html+="<li onclick=\"precontract.chooseAuto("+data[i]['cxId']+","+(step+01)+")\">"+
                        "<a href=\"javascript:void(0)\">"+
                        	"<i>&#xe61b;</i>"+
                            "<span>"+data[i]['name']+"</span>"+
                        "</a>"+
                    "</li>";
		}
		return html;
	},

	//转换车型数据成HTML
	getAutoListHtml:function(data,step){

		var arr=["brandId","cxModel","cx_displacement","cx_year"],
			html="",
			arrTemp={},
			len=data.length,
			i=0;

		if(step==3){
			html="<li style=\"font-size: 14px;text-align: center;color: #f60;\">该年份为您爱车的生产年份</li>";
		}

		for(;i<len;i++){
			if(typeof arrTemp[data[i][arr[step]]]=="undefined"){
	
				var even=step!=3?"precontract.chooseAuto('"+data[i][arr[step]]+"',"+(step+1)+")":"precontract.confirmAuto('"+data[i]["model_id"]+"','"+data[i]['cxName']+" "+data[i]['cxModel']+" "+data[i]['cx_displacement']+" "+data[i]["cx_year"]+"','"+data[i]["brand_logo"]+"')";
				html+="<li onclick=\""+even+"\">"+
                    	"<i>&#xe608;</i>"+
                        "<span>"+data[i][arr[step]]+"</span>"+
                    "</li>";
                arrTemp[data[i][arr[step]]]=true;
			}
			
		}
		
		return html;
	},

	//选择返回上步
	chooseAutoReturn:function(){
		var list=$("#autoSelect > .list"),
			step=list.attr("data-step");

		list.attr("data-step",step-1).css({"marginLeft":"-"+(step*200-200)+"px"}).next("a").show();
	},

	//确定车型
	confirmAuto:function(id,name){
		var list=$("#autoSelect > .list"),
			step=list.attr("data-step"),
			_t=this;

		list.attr("data-step",0).css({"marginLeft":"0px"});

		this.partInfoDom.find("#J_autoName").html(name);
		this.partInfoDom.find("input[name='model_id']").val(id);
		$("#autoSelect").removeClass("show");
		$("img.autoLogo").attr("src",'/'+arguments[2]);

		$.ajax({
			url:"/Index/getGoodsList",
			type:"post",
			dataType:"json",
			data:{"model_id":id},
			success:function(res){
				if(res.status==1){
					_t.printGoods(res.data);
				}else{
					alert(res.info);
				}
			}
		})

	},

	printGoods:function(data){
		var html="<span class='label'>机油：</span>",
            jiyouArr=data['jiyou']['jiyou'].split("||"),
            lenJY=jiyouArr.length,
            price="";
            i=0;

		for(;i<lenJY;i++){
			if(i!=0){
				html+="<span class='label'></span>";
			}
			html+='<div class="input">'+jiyouArr[i]+
                    '<a href="javascript:void(0)" class="chosen active">&#xe600;</a>'+
                '</div>';
		}
		price=data['jiyou']['price'];
		$("#J_jiyou").show().html(html);

		html="<span class='label'>机滤：</span>";
		html+='<div class="input">'+data['jilv']['name']+
                    '<a href="javascript:void(0)" class="chosen active">&#xe600;</a>'+
                '</div>';
        $("#J_jilv").show().html(html);

        price=price*1+data['jilv']['price']*1;
        this.partInfoDom.find(".total").html("￥"+price);
        price=this.isTS?"￥100":"￥"+price;
        this.partInfoDom.find(".cost").html("总价："+price);
	},

	isTianshi:function (){

	}

}

